package com.cly.controller;

import com.alibaba.fastjson.JSON;
import com.cly.constant.QueueConstant;
import com.cly.model.WxMsgModel;
import com.cly.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("p/sms")
@Api(tags = "会员发短信管理接口")
public class SendSmsController {

    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("send")
    @ApiOperation("发送短信验证吗")
    public ResponseEntity<String> sendSms(@RequestBody Map<String, String> map){
        //发送短信 记录数据库
        userService.send(map);
        return ResponseEntity.ok("发送成功，请在手机查看");
    }

    @PostMapping("savePhone")
    @ApiOperation("保存手机号")
    public ResponseEntity<String> savePhone(@RequestBody Map<String, String> map){
        userService.savePhone(map);
        return ResponseEntity.ok("保存成功");
    }

    /**
     * 测试发送微信公众号消息
     *
     * @param openId
     * @return
     */
    @GetMapping("testMyWx")
    public String testWxMsg(String openId) {
        // 1.创建一个微信模板对象
        WxMsgModel wxMsgModel = new WxMsgModel();
        wxMsgModel.setToUser(openId);
        wxMsgModel.setUrl("https://www.bilibili.com/");
        wxMsgModel.setTopColor("#173177");
        wxMsgModel.setTemplateId("REEYbqe9TBqhbXWtlC_i8kShc21n9UYTvnQZeYdc12c");
        HashMap<String, Map<String, String>> data = new HashMap<>(8);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        data.put("time", WxMsgModel.buildMap(sdf.format(new Date()), "#173177"));
        data.put("goods", WxMsgModel.buildMap("女朋友", "#173177"));
        data.put("money", WxMsgModel.buildMap("9.99", "#173177"));
        data.put("yue", WxMsgModel.buildMap("894,569,276.00", "#173177"));
        wxMsgModel.setData(data);
        // 放mq
        rabbitTemplate.convertAndSend(QueueConstant.WX_SMS_EX, QueueConstant.WX_SMS_KEY, JSON.toJSONString(wxMsgModel));
        return "ok";
    }

}
